package org.mspring.mlog.web.freemarker.directive;

import java.io.IOException;
import java.util.Map;

import org.mspring.mlog.utils.PostUrlUtils;
import org.mspring.platform.web.freemarker.DirectiveUtils;
import org.mspring.platform.web.freemarker.FreemarkerVariableNames;
import org.mspring.platform.web.freemarker.directive.AbstractDirectiveModel;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 文章绝对路径
 *
 * @author Gao Youbo
 * @since 2012-11-21
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class PostAbsUrlDirectiveModel extends AbstractDirectiveModel {

    @Override
    public void exec(Environment env, Map params, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {
        String base = DirectiveUtils.getItem(env, FreemarkerVariableNames.BASE).toString();
        Long id = DirectiveUtils.getLong("id", params);
        if (id != null) {
            String postUrl = PostUrlUtils.getPostAbsUrl(id);
            env.getOut().append(base + postUrl);
        }
    }
}
